#Alexander F. Gazmararian
#afg2@princeton.edu
#January 19, 2024

#Load packages
library(tidyverse)
library(tidylog)
library(viridis)
library(readxl)

#load data
g <- read_xlsx(here("data", "netmetering", "epa_04_10.xlsx"), skip = 3)
#Process data
g <- g[c(2:12),1:6]
names(g) <- tolower(gsub("\\.(.*)","",names(g)))
g <- mutate(g, across(year:total, ~ as.numeric(.x)))
#Create Figure A1: Net-metering growth in the US
p <- g %>%
  pivot_longer(cols = c(residential:total)) %>%
  filter(name!="transportation" & name!="total") %>%
  ggplot(aes(x=year,y=value,color=name,lty=name,shape=name)) +
  geom_line(linewidth = 1) +
  geom_point(size = 3) +
  theme_bw(base_size = 14) +
  labs(y="Capacity (MW)",x="Year",color="",lty="",shape="") +
  scale_x_continuous(breaks = seq(2011, 2021, 2)) +
  scale_y_continuous() +
  scale_color_viridis(discrete = TRUE) +
  theme(
    panel.grid = element_blank(),
    legend.position = c(.1,.9),
    legend.background = element_rect(fill='transparent'),
    legend.box.background = element_rect(fill='transparent',color="transparent")
  )
#Save figure
ggsave(
  p,
  filename = here("output", "figures", "netmeter_usa.png"),
  dpi = 300,
  scale = 1.5,
  width = 6,
  height = 3
)